﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.IO;
using System.Data;
using CoreShop.Model;

namespace CoreShop.App
{
    public class BillXml
    {
        public static string TaskID = "";

        public static void CreateNode(XmlDocument xmlDoc, XmlNode parentNode, string name, string value)
        {
            XmlNode node = xmlDoc.CreateNode(XmlNodeType.Element, name, value);
            node.InnerText = value;
            parentNode.AppendChild(node);

        }

        public static void CreateTaskXml()
        {
            string filepath = Environment.CurrentDirectory + "\\xml\\task.xml";
            if (File.Exists(filepath))
                File.Delete(filepath);

            TaskID = Common.GetTaskID();
            XmlDocument xmlDoc = new XmlDocument();
            XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");
            xmlDoc.AppendChild(node);

            XmlNode root = xmlDoc.CreateElement("MTTask");
            xmlDoc.AppendChild(root);
            CreateNode(xmlDoc, root, "TaskID", TaskID);
            CreateNode(xmlDoc, root, "TaskType", "0");
            CreateNode(xmlDoc, root, "DataFile", "DeviceList.xml");
            try
            {
                xmlDoc.Save(filepath);
            }
            catch (Exception e)
            {
                //显示错误信息
                throw e;
            }
        }

        public static void CreateDeviceXml(string IP, string Port, string Type, string ID, string No)
        {
            string filepath = Environment.CurrentDirectory + "\\xml\\devicelist.xml";

            string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
                + "<Devices>"
                    + "<Scale>"
                        + "<DeviceID>" + ID + "</DeviceID>"
                        + "<ScaleNo>" + No + "</ScaleNo>"
                        + "<ScaleType>bCom</ScaleType>"
                        + "<ConnectType>Network</ConnectType>"
                        + "<ConnectParams>"
                            + "<NetworkParams Port=\"" + Port + "\" Address=\"" + IP + "\"/>"
                        + "</ConnectParams>"
                        + "<DecimalDigits>2</DecimalDigits>"
                        + "<Enabled>true</Enabled>"
                        + "<DataFile>Command1.xml</DataFile>"
                    + "</Scale>"
                + "</Devices>";
            File.WriteAllText(filepath, xml);
        }

        public static string CheckResult()
        {
            string filepath = Environment.CurrentDirectory + "\\xml\\taskResult.xml";
            XmlDocument doc = new XmlDocument();

            doc.Load(filepath);

            string status = doc.SelectSingleNode("/MTTaskResult/TaskStatus").InnerText;
            if (status.Equals("Error"))
                return doc.SelectSingleNode("/MTTaskResult/ErrorMessage").InnerText;
            else if (status.ToLower().Equals("complete"))
                return "1";
            else
                return status;
        }

        public static void CreateDataXml(List<corecmsfoodspec> goodsList)
        {
            string filepath = Environment.CurrentDirectory + "\\xml\\Data1.xml";
            StringBuilder xmlstr = new StringBuilder();            
            xmlstr.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            xmlstr.Append("<Data>");

            foreach (var item in goodsList)
            {
                xmlstr.Append("<Item>");
                xmlstr.Append("<PLU>" + item.weighPlu.ToString() + "</PLU>");
                xmlstr.Append("<AlternativeItemIDs><AlternativeItemID>" + item.number.ToString() + "</AlternativeItemID></AlternativeItemIDs>");
                xmlstr.Append("<Descriptions><Description Index=\"0\" Type=\"ItemName\" Language=\"zho\" ID=\"0\">"
                    + item.name.ToString() + "</Description><Description Type=\"ExtraText\" Language=\"zho\" ID=\"1\"/></Descriptions>");
                xmlstr.Append("<ItemPrices><ItemPrice Index=\"0\" Currency=\"CNY\" Quantity=\"0\" DiscountFlag=\"false\" PriceOverrideFlag=\"false\" UnitDes=\"\" UnitOfMeasureCode=\"KGM\">"
                    + item.posPrice.ToString() + "</ItemPrice></ItemPrices>");
                xmlstr.Append("<LabelFormats><LabelFormatID Index=\"0\">1</LabelFormatID></LabelFormats>");
                xmlstr.Append("</Item>");
            }
            xmlstr.Append("</Data>");

            File.WriteAllText(filepath, xmlstr.ToString());
        }

        public static void CreateCommandXml()
        {
            string filepath = Environment.CurrentDirectory + "\\xml\\Command1.xml";
            StringBuilder xmlstr = new StringBuilder();
            xmlstr.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

            xmlstr.Append("<Commands>");
                xmlstr.Append("<Command>");
                    xmlstr.Append("<CommandText>Item</CommandText>");
                    xmlstr.Append("<CommandID>" + System.Guid.NewGuid().ToString() + "</CommandID>");
                    xmlstr.Append("<Control>Update</Control>");
                    xmlstr.Append("<ClearData>false</ClearData>");
                    xmlstr.Append("<DataFile>Data1.xml</DataFile>");
                xmlstr.Append("</Command>");
            xmlstr.Append("</Commands>");

            File.WriteAllText(filepath, xmlstr.ToString());
        }
    }
}
